package com.hehao.java_basic.thread;



/**
 * @author Damon
 * @date 2021/12/20
 */
public class CreateThread {
    /**
     * main线程和自建线程区别
     * @param args
     */
    public static void main(String[] args) {
        System.out.println("main start...");
        Thread t = new Thread() {
            @Override
            public void run() {
                System.out.println("thread run...");
                try {
                    Thread.sleep(10);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println("thread end.");
            }
        };
        t.start();
        try {
            Thread.sleep(20);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("main end...");
    }

    /**
     * 1.继承Thread，重写run
     */
    static class MyThread extends Thread {
        @Override
        public void run() {
            System.out.println("继承创建thread，并有独立执行体！");
        }
    }

    /**
     * 2.实现runnable，重写run
     */
    static class MyRunnable implements Runnable {
        @Override
        public void run() {
            System.out.println("实现runnable接口，并有独立执行体！");
        }
    }

    /**
     * 3.lambda接口实现runnable，重写run
     */
    public void lambdaRunnable() {
        Thread thread = new Thread(()->{
            System.out.println("lambda实现runnable接口，并有独立执行体！");
        });
        thread.start();
    }


}
